package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;
    @Autowired
    private JedisPool jedis;

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) {
        //解析上传文件的内容
        try {
            List<String[]> excelList = POIUtils.readExcel(excelFile);
            //把解析的内容放入到Ordersetting类中
            List<OrderSetting> orderSettingList = new ArrayList<OrderSetting>();

            //遍历excelList
            if(excelFile != null && excelList.size() > 0) {
                for (String[] str : excelList) {
                    //创建OrderSetting对象
                    OrderSetting orderSetting = new OrderSetting(new Date(str[0]),
                            Integer.parseInt(str[1]));
                    orderSettingList.add(orderSetting);
                }
            }

            orderSettingService.add(orderSettingList);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    @RequestMapping("/findBydate")
    public Result findBydate(String date) {
        try {
            List<Map<String,Object>> orderSettingList = orderSettingService.findBydate(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,orderSettingList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/editnumberBydate")
    public Result editnumberBydate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editnumberBydate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/setReservations")
    public Result setReservations(String reservation) {
        try {
            Date date = new Date();
            String format = DateUtils.parseDate2String(date,"yyyy-MM");
            String data = JSON.toJSONString(format+","+reservation);
            jedis.getResource().set("setReservation",data);
            jedis.close();
            return new Result(true,MessageConstant.SET_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SET_ORDERSETTING_FAIL);
        }
    }
}
